Skip to content

Comments

gh-141395: Clarify stdout flush behavior for newline characters in print()#142094

Closed
Vemulakonda559 wants to merge 10 commits intopython:mainfrom
Vemulakonda559:Vemulakonda559-patch-1
Closed

gh-141395: Clarify stdout flush behavior for newline characters in print()#142094
Vemulakonda559 wants to merge 10 commits intopython:mainfrom
Vemulakonda559:Vemulakonda559-patch-1

Conversation

@Vemulakonda559
Copy link

@Vemulakonda559 Vemulakonda559 commented Nov 29, 2025

This PR adds a note to the print() documentation to clarify how Python’s stdout buffering works with newline (\n) characters inside a single print call.

Motivation:

Current documentation mentions that flush() is implied for writes containing newlines.

However, it does not explain that Python flushes only after the entire write operation, not mid-string.

This can confuse users coming from C, who expect a flush at each newline, and developers writing scripts that rely on immediate output for progress indicators or CLI feedback.

What’s added:

A .. note:: block explaining that stdout behavior depends on the environment (TTY vs redirected stdout).

Guidance on explicitly flushing with flush=True or sys.stdout.flush().

Mention of python -u for unbuffered output.

A short example demonstrating the behavior.

Impact:

Improves clarity for learners and developers.

Aligns documentation with actual behavior across different environments.

Not a behavior change — documentation-only PR.

Related Issue:

Addresses issue #141395


📚 Documentation preview 📚: https://cpython-previews--142094.org.readthedocs.build/

@Vemulakonda559 Vemulakonda559 force-pushed the Vemulakonda559-patch-1 branch from 2d66675 to c3c6dab Compare January 1, 2026 05:09
@Vemulakonda559
Copy link
Author

Hello! I’ve updated the branch to be up to date with main.
Thank you for the initial review. I’d appreciate a follow-up review when you have time, and I’m happy to address any further feedback.

Vemulakonda559 and others added 10 commits January 2, 2026 09:25
This PR adds a note to the print() documentation to clarify how Python’s stdout buffering works with newline (\n) characters inside a single print call.

Motivation:

Current documentation mentions that flush() is implied for writes containing newlines.

However, it does not explain that Python flushes only after the entire write operation, not mid-string.

This can confuse users coming from C, who expect a flush at each newline, and developers writing scripts that rely on immediate output for progress indicators or CLI feedback.


What’s added:

A .. note:: block explaining that stdout behavior depends on the environment (TTY vs redirected stdout).

Guidance on explicitly flushing with flush=True or sys.stdout.flush().

Mention of python -u for unbuffered output.

A short example demonstrating the behavior.


Impact:

Improves clarity for learners and developers.

Aligns documentation with actual behavior across different environments.

Not a behavior change — documentation-only PR.


Related Issue:

Addresses issue python#141395
Removed duplicate text and improved clarity in the note about stdout flushing behavior.
Removed redundant sentence about output buffering.
Removed unnecessary comments about string flushing in print.
Remove extra blank lines and fix indentation for versionchanged directive.
Reformat output buffering explanation for clarity.
@Vemulakonda559 Vemulakonda559 force-pushed the Vemulakonda559-patch-1 branch from c3c6dab to 0ece9e0 Compare January 2, 2026 03:55
@Vemulakonda559
Copy link
Author

Hi, kindly requesting a review when time permits. I’m happy to update the PR based on any feedback.

Output buffering is usually determined by *file*. However, if *flush* is
true, the stream is forcibly flushed.

.. note::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, this note is unnecessary. Python never intended to behave as C does. We've already said that flush forcibly flushes.

@merwok
Copy link
Member

merwok commented Jan 18, 2026

I agree with Ned.

@merwok merwok closed this Jan 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

4 participants